<template>
  <div>
    <el-row>
      <el-col :span="4">
        <div class="tree">
          <el-input placeholder="输入关键字进行过滤" v-model="filterText">
          </el-input>
          <el-tree @node-click="handler" class="filter-tree" :data="empData" :props="defaultProps"
            :filter-node-method="filterNode" ref="tree">
          </el-tree>
        </div>
      </el-col>
      
      <!-- 右侧 -->
      <el-col :span="20">
        <div v-if="params.device_id">
          
          <!-- 日历搜索 -->
          <el-form :inline="true" :model="params" class="date-time-form">
            <el-form-item>
              <el-date-picker
                v-model="dateTimeRange"
                type="datetimerange"
                range-separator="至"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                :picker-options="pickerOptions"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                :default-time="['00:00:00', '23:59:59']"
                class="custom-date-picker"
              >
              </el-date-picker>
            </el-form-item>
            <!-- 搜索按钮 -->
            <el-form-item>
              <el-button type="primary" @click="toSearch">搜索</el-button>
            </el-form-item>
          </el-form>

          <!-- 数据 -->
          <el-table :data="monitors" style="width: 100%">
            <el-table-column prop="insert_time" label="日期" align="center">
              <template #default="{ row }">
                {{ formatTimestamp(row.insert_time) }}
              </template>
            </el-table-column>
            <el-table-column prop="tsp" label="tsp" align="center"></el-table-column>
            <el-table-column prop="pm10" label="pm10" align="center"></el-table-column>
            <el-table-column prop="pm25" label="pm2.5" align="center"></el-table-column>
            <el-table-column prop="temperature" label="温度" align="center"></el-table-column>
            <el-table-column prop="humidity" label="湿度" align="center"></el-table-column>
            <el-table-column prop="wind_direction" label="风向" width="300"></el-table-column>
            <el-table-column prop="wind_speed" label="风速" align="center"></el-table-column>
          </el-table>

          <!-- 分页 -->
          <div class="page" style="margin-top:10px;">
      <!-- hide-on-single-page 分页数据只有一页隐藏分页按钮 -->
      <el-pagination  background 
        @current-change="handleCurrentChange" :current-page="params.page" :page-sizes="15"
        :page-size="params.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

        </div>
        <div v-else style="text-align: center; color: red; line-height: 50px">
          请选择设备进行查看
        </div>
      </el-col>
      
    </el-row>
  </div>
</template>

<script>
import { get } from "@/utils/request";
// import moment from "moment";

export default {
  data() {
    return {
      filterText: '',
      empData: [],
      defaultProps: {
        children: 'children',
        // 节点标签对应对象属性值
        label: 'name'
      },
      params: {
        page:1,
        pageSize:15,
        device_id: 0,
        engineer_id: 0,
        
      },
      total: 0,
      // 表格数据源
      monitors: []
    }
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val);
    },

    
  },
  methods: {
    // 时间格式转换
    formatTimestamp(timestamp) {
  return new Date(timestamp).toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit'
  })
},

    // 处理当前页码改变方法 1页---2页
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.params.page = val;
      // 重新查询数据
      this.getMonitor();
    },

    toSearch() {
      this.getMonitor();
    },

    handler(val, node) {
      if (!val.children) {
        this.params.device_id = val.id;
        this.params.engineer_id = val.engineer_id;
        // 发起查询请求 
        this.getMonitor();
      }
    },
    async getMonitor() {
      let res = await get('/monitor/pageQuery', this.params);
      // console.log(res,'获取响应');
      this.monitors = res.data.list;
      this.total = res.data.total;
    },
    // 过滤节点 
    filterNode(value, data) {
      // console.log(value, data)  value输入框值 data根据值过滤出来的对象
      if (!value) return true;
      return data.name.indexOf(value) !== -1;
    },
    async getEmpData() {
      let res = await get('/dashboard/findEngineerDeviceTree');
      this.empData = res.data;
    }
  },
  created() {
    this.getEmpData();
  }
};
</script>

<style scoped>

.tree {
  border-right: 1px solid #ebeef5;
  padding-right: 20px;
  min-height: 600px;
}

.filter-tree {
  margin-top: 10px;
}
</style>    